﻿package bacIns
{	
	
	import bacIns.exchangeddata.BacBetInfo;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import game.BetController;
	import game.BetPlace;
	import game.events.EventData;
	import game.Chip;
	import common.Utility;	
	/**
	 * ...
	 * @author Nguyen Thi Bich Phuong (Daisy)
	 */
	public class BacBetController extends BetController {				
		public var bankerHit:SimpleButton;
		public var playerHit:SimpleButton;	
		public var tieHit:SimpleButton;
		public var bankerPairHit:SimpleButton
		public var playerPairHit:SimpleButton;
		public var bigHit:SimpleButton;
		public var smallHit:SimpleButton;
		public var playerInsHit:SimpleButton;
		public var bankerInsHit:SimpleButton;
		
		public var bankerBet:BetPlace;
		public var playerBet:BetPlace;
		public var tieBet:BetPlace;
		public var bankerPairBet:BetPlace;
		public var playerPairBet:BetPlace;
		public var bigBet:BetPlace;
		public var smallBet:BetPlace;		
		public var playerInsBet:BetPlace;
		public var bankerInsBet:BetPlace;
		
		public var betInfo:BacBetInfo;
		public var saveBetInfo:BacBetInfo;
		public var preBetInfo:BacBetInfo;	
		
		public const BANK_INS:String = "BI";
		public const PLAYER_INS:String = "PI";
		
		public function BacBetController() {
			super.rebetBtn.visible = false;
			super.txtRebetTf.visible = false;
			
			this.betInfo = new BacBetInfo();
			this.saveBetInfo = new BacBetInfo();
			this.preBetInfo = new BacBetInfo();
			this.hitArr = [this.bankerHit, this.playerHit, this.tieHit, this.bankerPairHit, this.playerPairHit, this.bigHit, this.smallHit, this.bankerInsHit, this.playerInsHit];			
		}
		
		/**
		 * @author Daisy
		 * @param	minBet
		 * @param	maxBet
		 * @param	currentBalance
		 * @param	initChipValueArr
		 */
		 override public function initBet(minBet:Number, maxBet:Number, currentBalance:Number, initChipValueArr:Array):void {			
			super.initBet(minBet, maxBet, currentBalance, initChipValueArr);
			
			var newBetArr:Array = super.initBetPlace(this.hitArr);						
			
			this.bankerBet = newBetArr[0];
			this.playerBet = newBetArr[1];
			this.tieBet = newBetArr[2];
			this.bankerPairBet = newBetArr[3];
			this.playerPairBet = newBetArr[4];
			this.bigBet = newBetArr[5];
			this.smallBet = newBetArr[6];
			this.bankerInsBet = newBetArr[7];
			this.playerInsBet = newBetArr[8];
			
			this.betArr = [this.bankerBet, this.playerBet, this.tieBet, this.bankerPairBet, this.playerPairBet, this.bigBet, this.smallBet, this.bankerInsBet, this.playerInsBet];		
		}
		
		/**
		 * @author Daisy
		 * @param	e
		 */
		override public function confirmBtnClickHandler(e:MouseEvent):void {
			dispatchEvent(new EventData(EventData.CONFIRM_BETTING, true));		
		}	
		
		/**
		 * @author Daisy 
		 */
		public function saveBetting():void {
			for (var i:int = 0; i < this.betInfo.arrBettingValue.length; i++ ) {
				this.betInfo.arrBettingValue[i] = BetPlace(this.betArr[i]).getBetValue();				
			}
		}
		/**
		 * @author Daisy
		 * @param	e
		 */
		override public function clearBtnClickHandler(e:MouseEvent):void {
			var tmpBetting:Number = 0;
			tmpBetting = this.getTotalBetValue() - this.saveBetInfo.getTotalBet();			
			if (tmpBetting > 0) {						
				currentBalance = Utility.fixNumber(currentBalance + tmpBetting);
				var event:EventData = new EventData(EventData.CLEAR_BETTING, true);				
				event.newBalance = currentBalance;	
				dispatchEvent(event);				
			}		
		}
		
		/**
		 * @author Daisy
		 * @param	e		 
		 */
		override public function rebetBtnClickHandler(e:MouseEvent):void {		
			if (this.preBetInfo.getTotalBet() > 0) {
				//set currentBalance clear before rebet
				var tmpBetting:Number = 0;
				tmpBetting = this.getTotalBetValue() - this.saveBetInfo.getTotalBet();
				currentBalance = Utility.fixNumber(currentBalance + tmpBetting);
				//set currentBalance after rebet
				currentBalance = Utility.fixNumber(currentBalance - this.preBetInfo.getTotalBet());
				var event:EventData = new EventData(EventData.REBET_BETTING, true);
				event.newBalance = currentBalance;	
				dispatchEvent(event);				
			}			
		}
		
		/**
		 * @author Daisy
		 * @param	e
		 * @usage update value betting		 
		 */
		override public function updateBet(info:Object):void {
			this.betInfo = BacBetInfo(info);	
			for (var i:int = 0; i < this.betInfo.arrBettingCode.length; i++) {
				BetPlace(this.betArr[i]).setBetValue(this.betInfo.arrBettingValue[i]);
			}
		}
				
		public function lockIns(betIns:String) {			
			if (betIns == "") {				
				BetPlace(this.betArr[7]).betHit.mouseEnabled = false;
				BetPlace(this.betArr[8]).betHit.mouseEnabled = false;
				SimpleButton(BetPlace(this.betArr[7]).betHit).useHandCursor = false;
				SimpleButton(BetPlace(this.betArr[8]).betHit).useHandCursor = false;
			} else {
				this.lock();
				this.isLock = false;				
				confirmBtn.mouseEnabled = true;
				clearBtn.mouseEnabled = true;
				rebetBtn.mouseEnabled = true;				
				for (var j = 0; j < this.chipController.chipArr.length; j++) {					
					Chip(this.chipController.chipArr[j]).mouseEnabled = true;
				}
				if (betIns == this.BANK_INS) {
					BetPlace(this.betArr[7]).betHit.mouseEnabled = true;				
					SimpleButton(BetPlace(this.betArr[7]).betHit).useHandCursor = true;				
				} else if (betIns == this.PLAYER_INS) {
					BetPlace(this.betArr[8]).betHit.mouseEnabled = true;
					SimpleButton(BetPlace(this.betArr[8]).betHit).useHandCursor = true
				}				
			}			
		}		
	}
}